package util;


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;

public class Log4jInit extends HttpServlet {
	
	@Override
	public void init() throws ServletException {
		 String path = this.getServletContext().getRealPath("/");
		 String log4j_name = getInitParameter("log4j");
		 String src = path+"WEB-INF"+File.separator+"classes"+ File.separator + log4j_name;
		 System.out.println("log4j配置文件路径："+src);
		 Properties props = new Properties();
		 try
		 {
			 FileInputStream log4jStream = new FileInputStream(src);
		     props.load(log4jStream);
		     log4jStream.close();
		     String infoLogFile = path+"WEB-INF"+File.separator+"logs\\c2c.log";
			 String debugLogFile = path+"WEB-INF"+File.separator+"logs\\c2c_Debug.log";
			 String errorLogFileString = path+"WEB-INF"+File.separator + "logs\\c2c_err.log";
	         System.out.println("日志文件路径:"+infoLogFile);
	         props.setProperty("log4j.appender.InfoLog.File", infoLogFile);
	         props.setProperty("log4j.appender.DebugLog.File", debugLogFile); // //装入log4j配置信息	
	         props.setProperty("log4j.appender.ErrorLog.File", errorLogFileString); //装入log4j配置信息	
//			 System.out.println("log4j_src = "+src);
			if(log4j_name!=null)
			{
				PropertyConfigurator.configure(props);		
			}else{
			      BasicConfigurator.configure();
		    }
		 }catch(Exception e)
		 {
			 System.out.println("log4j配置文件装载文件出错");
		 }
		 super.init();
	}
//	@Override
//	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
//			throws ServletException, IOException {
//	}
//
//	@Override
//	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
//			throws ServletException, IOException {
//		this.doGet(req, resp);
//	}
//	
//	
//	public static void main(String[] args)
//	{
//		System.out.println("aa");
//	}
	
	

}
